package com.baylor.ui;

import com.baylor.ui.fragment.FirstFragment;
import com.baylor.ui.fragment.SecondFragment;
import com.baylor.ui.fragment.ThirdFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class FragmentTabHostActivtiy extends FragmentActivity {
	
	private FragmentTabHost mTabHost;
	private ImageView mImgSign;
	private TextView mTitle;

	private Class<?> mTabSpecFragments[] = {
				FirstFragment.class,
				SecondFragment.class,
				ThirdFragment.class };

	private int[] mTabSpecBackgroundImgs = {
				R.drawable.selector_tab_first,
				R.drawable.selector_tab_second,
				R.drawable.selector_tab_third };
	
	private String[] mTabSpecTags = { 
				"first", 
				"second", 
				"third" };
	
	private int[] mTabSpecIcons = {
			R.drawable.ic_tab_first,
			R.drawable.ic_tab_second,
			R.drawable.ic_tab_third
	};
	
	private String mTabSpecNames[] = { "传感", "监控", "信息" };

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		
		setContentView(R.layout.activity_main);
		findView();
		afterViews();
	}
	
	private void findView() {
		mTabHost = (FragmentTabHost)findViewById(R.id.tabhost);
		mImgSign = (ImageView)findViewById(R.id.img_sign);
		mTitle = (TextView)findViewById(R.id.txt_title);
	}
	
	private void afterViews() {
		mTitle.setText(mTabSpecNames[0]);
		
		initTabHost();
		// 若不需要一直存在 TabHost ,那么不需要执行 setListener() 方法
		setListener();
	}

	private void initTabHost() {
		// 设置 TabHost
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		
		int count = getTabItemsCount();
		for (int i = 0; i < count; i++) {
			// 为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTabSpecTags[i]);
			tabSpec.setIndicator( getTabItemIndicator(i) );
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, mTabSpecFragments[i], null);
		}
	} 
	
	private View getTabItemIndicator(int index) {
		View tabItem = View.inflate(this, R.layout.item_tab_view, null);
		tabItem.setBackgroundResource(mTabSpecBackgroundImgs[index]);
		((TextView)tabItem.findViewById(R.id.tab_name)).setText(mTabSpecNames[index]);
		((ImageView)tabItem.findViewById(R.id.tab_icon)).setImageResource(mTabSpecIcons[index]);
		return tabItem;
	}
	
	private void setListener() {
		int count = getTabItemsCount();
		// 为每个 tab 设置点击事件
		for (int i = 0; i < count; i++) {
			final int j = i;
			View tabItemView = mTabHost.getTabWidget().getChildAt(i);
			tabItemView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// 在用户点击 底部的 tab 后，移除其他 tab 的 fragment 栈,并跳转到指定的 tab
					setCurrentTabAndPopBackAllStack(j);
					
					// 更新title名
					mTitle.setText(mTabSpecNames[j]);
				}
			});
		}
	}
	
	private int getTabItemsCount() {
		return mTabSpecFragments.length;
	}
	
	private void setCurrentTabAndPopBackAllStack(int currentTabIndex) {
		popBackAllStack();
		mTabHost.setCurrentTab( currentTabIndex );
	}
	
	public void popBackAllStack() {
		getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
	
	public void setOnline(boolean flag) {
		if(mImgSign != null) {
			if(flag) {
				mImgSign.setImageResource(R.drawable.ic_online_yes);
			}else{
				mImgSign.setImageResource(R.drawable.ic_online_no);
			}
		}
	}
}
